package com.clas.liu.myapplication.model.impl;

import android.os.Handler;

import com.clas.liu.myapplication.model.entity.BaseRoot;
import com.clas.liu.myapplication.model.entity.info.InfoRoot;
import com.clas.liu.myapplication.model.entity.user.UserRoot;
import com.clas.liu.myapplication.model.event.UserEvent;
import com.clas.liu.myapplication.model.event.UserInfoEvent;
import com.clas.liu.myapplication.model.i.IUserInfoModel;
import com.clas.liu.myapplication.network.NetworkInterfaces;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by liu on 2019/5/6.
 */

public class UserInfoModel extends BaseModel implements IUserInfoModel {


    public UserInfoModel(Handler handler) {
        super(handler);
    }

    @Override
    public void getUser(int userId) {
        Callback callback = new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                UserInfoEvent event = new UserInfoEvent();
                event.setWhat(UserInfoEvent.GET_USER_INFO_USER_FAIL);
                postEvent(event);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()) {
                    Gson gson = new Gson();
                    UserRoot root = gson.fromJson(response.body().string(), UserRoot.class);  //json解析
                    UserInfoEvent event = new UserInfoEvent();                        //事件
                    event.setWhat(UserInfoEvent.GET_USER_INFO_USER_OK);                         //事件的设置
                    event.setUserRoot(root);
                    postEvent(event);
                }else
                    onFailure(call, new IOException(response.body().string()));
            }
        };
        interfaces.getUserById(userId,callback);
    }

    @Override
    public void getInfo(int userId, int type, int page_no, int page_size) {
        Callback callback = new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                UserInfoEvent event = new UserInfoEvent();
                event.setWhat(UserInfoEvent.GET_USER_INFO_INFO_FAIL);
                postEvent(event);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()) {
                    Gson gson = new Gson();
                    InfoRoot root = gson.fromJson(response.body().string(), InfoRoot.class);  //json解析
                    UserInfoEvent event = new UserInfoEvent();                        //事件
                    event.setWhat(UserInfoEvent.GET_USER_INFO_INFO_OK);                         //事件的设置
                    event.setInfoRoot(root);
                    postEvent(event);
                }else
                    onFailure(call, new IOException(response.body().string()));
            }
        };
        interfaces.getUserInfo(userId,type,page_no,page_size,callback);
    }

    @Override
    public void jubaoInfo(int userId, String text, String pic,String releaseTime) {
        Callback callback = new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                UserInfoEvent event = new UserInfoEvent();
                event.setWhat(UserInfoEvent.JUBAO_USER_FAIL);
                postEvent(event);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()) {
                    Gson gson = new Gson();
                    BaseRoot root = gson.fromJson(response.body().string(), BaseRoot.class);  //json解析
                    UserInfoEvent event = new UserInfoEvent();                        //事件
                    event.setWhat(UserInfoEvent.JUBAO_USER_OK);                         //事件的设置
                    event.setBaseRoot(root);
                    postEvent(event);
                }else
                    onFailure(call, new IOException(response.body().string()));
            }
        };
        interfaces.jubaoUser(userId,text,pic,releaseTime,callback);
    }

    @Override
    protected NetworkInterfaces getNetworkInterface() {
        return new NetworkInterfaces();
    }
}
